﻿using AtKardesler.UIProcess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Xml;
using AtKardesler.Data;
public partial class Login : BasePage
{
    public override void GetStateFromUI()
    {

    }

    public override void SetUIFromState()
    {

    }
    //Login implementasyonu yapılacak WebService ile sonra homepage yönelecek
    public void Login_ButtonClick(object sender, EventArgs e)
    {
        if (ValidateUser(txtuserName.Text, txtPasword.Text))
        {
            FormsAuthentication.Initialize();
            String strRole = AssignRoles(txtuserName.Text);
            FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, txtuserName.Text, DateTime.Now, DateTime.Now.AddMinutes(30), false, strRole, FormsAuthentication.FormsCookiePath);
            Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat)));
            Response.Redirect(FormsAuthentication.GetRedirectUrl(txtuserName.Text, false));
        }

    }

    private Boolean ValidateUser(String strUsername, String strPassword)
    {
        bool isValid = false;
       
        if (this.Users != null && this.Users.Length > 0)
            foreach (AtKardesler.Data.User _user in this.Users)
            {
                if (_user.Username == strUsername && _user.Password == Int32.Parse(strPassword))
                {
                    isValid = true;
                    break;
                }
            }
        return isValid;
    }

    private String AssignRoles(String strUsername)
    {
        if (txtuserName.Text == "egemen")
            return "admin";
        else
            return String.Empty;
    }
}